package databasetest;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Update extends JInternalFrame {
	Connection conn;
	DatabaseTest main;
	
	private JLabel lblNewLabel;
	private JTextField serial;
	private JLabel lblEa;
	private JTextField code;
	private JLabel lblEa_1;
	private JTextField ea;
	private JButton btnNewButton;
	private JButton button;
	private JPanel jpan;
	private JLabel status;

	/**
	 * Create the frame.
	 */
	public Update(Connection c, DatabaseTest m) {
		super("수정", true, true, true, false);
		setBounds(100, 100, 334, 223);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerial());
		getContentPane().add(getLblEa());
		getContentPane().add(getCode());
		getContentPane().add(getLblEa_1());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getJpan());
		this.conn = c;
		this.main = m;
	}
	
	public void setData(String s, String c, String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}
	
	public void select(){
		String sql = "SELECT * FROM PTBL WHERE SERIAL = ?";
		
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
		    pstmt.setInt(1, Integer.parseInt(serial.getText()));
		    ResultSet rs = pstmt.executeQuery();					   
		    if(rs.next()){ // 데이터가 있다는 얘기..
		    	code.setText(rs.getString("code"));
		    	ea.setText(""+ rs.getInt("ea"));					    	
		    	status.setText("검색되었습니다.");
		    }else{
		    	status.setText("수정 할 데이터가 없습니다.");
		    	code.setText("");
		    	ea.setText("");
		    	serial.requestFocus();
		    }
		    
		    rs.close();
		    pstmt.close();
		} catch (Exception e2) {
			// TODO: handle exception
		}
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(81, 7, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("code");
			lblEa.setBounds(12, 38, 57, 15);
		}
		return lblEa;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(81, 35, 116, 21);
		}
		return code;
	}
	private JLabel getLblEa_1() {
		if (lblEa_1 == null) {
			lblEa_1 = new JLabel("ea");
			lblEa_1.setBounds(12, 66, 57, 15);
		}
		return lblEa_1;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(81, 63, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					select();
				}
			});
			btnNewButton.setBounds(209, 6, 70, 23);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String c = code.getText();
					int ee = Integer.parseInt(ea.getText());
					int s = Integer.parseInt(serial.getText());
					String sql = "UPDATE PTBL SET CODE = ?, EA = ? WHERE SERIAL = ?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, ee);
						pstmt.setInt(3, s);
						
						pstmt.executeUpdate();
						status.setText("수정되었습니다.");
						
						pstmt.close();
						
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
			});
			button.setBounds(81, 94, 97, 23);
		}
		return button;
	}
	private JPanel getJpan() {
		if (jpan == null) {
			jpan = new JPanel();
			jpan.setBackground(SystemColor.info);
			jpan.setBounds(12, 140, 294, 21);
			jpan.add(getStatus());
		}
		return jpan;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요.");
		}
		return status;
	}
}
